﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XChip_Common
{
    class IniClass
    {
        /// <summary>
        /// 用于Ini文件转成16进制数据文件
        /// </summary>
        /// <param name="iniPath"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static byte[] Ini2Data(string iniPath, string fileName)
        {
            //"@="表示文件名存放区，"~="表示ini存放区，"|"表示当前区结束符
            byte[] utf8Bytes = new byte[] { 0x40, 0x3d };//"@="
            utf8Bytes = utf8Bytes.Concat(Encoding.UTF8.GetBytes(fileName)).ToArray();
            utf8Bytes = utf8Bytes.Concat(Encoding.UTF8.GetBytes("|$=")).ToArray();

            if (!String.IsNullOrEmpty(iniPath))
            {
                StringBuilder contentBuilder = new StringBuilder();
                string[] lines = File.ReadAllLines(iniPath, Encoding.UTF8);
                foreach (string line in lines)
                {
                    contentBuilder.AppendLine(line);
                }
                string content = contentBuilder.ToString();
                byte[] iniText = Encoding.UTF8.GetBytes(content);
                utf8Bytes = utf8Bytes.Concat(iniText).ToArray();
                utf8Bytes = utf8Bytes.Concat(Encoding.UTF8.GetBytes("|~=")).ToArray();
            }
            return utf8Bytes;
        }
    }
}
